Ext.namespace("Doc");

Doc.Edit = function(p) {
	this.parentWin=p;	
	this.userSelector = null;
	this.winMsg=null;
	
	this.txtRemind=new Ext.form.Hidden({name:'remind'});
	this.hdSuperviseId = new Ext.form.Hidden({name:'superviseId'});
	this.txtNum = new Ext.form.TextField({
        fieldLabel: '公文编号',
        name:'docNum',
        width: 150,
        maxLength:40
    });
    this.txtIssueType = new Ext.form.ComboBox({
		width: 150,
		fieldLabel: '公文类别',
		name:'issueType',
        store: new Ext.data.ArrayStore({
        	fields: ['value', 'name'],
        	data: OMS.DOC_TYPE2
        }),
        valueField: 'value',
        displayField: 'name',
        value:0,
        editable: false,
        mode: 'local',
        triggerAction: 'all'
	});	
	this.txtTitle = new Ext.form.TextField({
        fieldLabel: '公文名称',
        name:'superviseTitle',
        width: 360,
        maxLength:40
    });
	this.txtManagerId=new Ext.form.Hidden({name:'managerId'});
	this.txtManagerName = new Ext.form.TextField({
		fieldLabel: '阅处负责人',
		name:'managerName',
		width: 340,
		readOnly: true,
		allowBlank: false,
        blankText: '请选择阅处负责人!',
		listeners: {
			   'render': function(cmp) {
					var self = cmp.ownerCt.ownerCt.ownerCt.ownerCt;
					cmp.getEl().on('click', function(){ self.showUser();}, self); 
				}
			}
	});	
	
	this.txtSuperviseContent = new Ext.form.TextArea({
		fieldLabel: '公文内容',
		name:'superviseContent',
		width: 360,
		height: 45,
		maxLength:1000
	});
	this.txtNotion = new Ext.form.TextArea({
		fieldLabel: '领导批示意见',
		name:'notion',
		width: 360,
		height: 45,
		allowBlank: false,
        blankText: '请输入领导批示意见!',
		maxLength:1000
	});
	this.form = new Ext.FormPanel({
		bodyStyle: 'padding: 5px 5px 0',
        labelWidth: 75,
        labelAlign: 'right',
        items: [ this.txtTitle,this.txtManagerId,this.txtRemind,this.hdSuperviseId,
		        { layout: 'column', border: false, defaults: {layout: 'form', border: false},
			        	items: [ { columnWidth: .50, items: [this.txtNum]},
			        	         { columnWidth: .50, items: [this.txtIssueType]}]
			    },this.txtSuperviseContent,this.txtNotion,
			    { layout: 'column', border: false, defaults: {layout: 'form', border: false},
	        	items: [ { columnWidth: 1, items: [this.txtManagerName]}]
	    	}]
    });
    this.btnSave = new Ext.Button({
    	text:'保存',
    	handler:this.onSave,
    	scope: this
    });
	Doc.Edit.superclass.constructor.call(this, {
		title: '新建公文',
        autoHeight: false,
        width: 500,
        y: 20,
        resizable: false,
        plain: true,
        modal: true,
        autoScroll: true,
        closeAction: 'hide',
        items: [this.form],
        buttons: [this.btnSave,{
            text: '关闭',
            scope: this,
            handler: function() {
	        	this.hide();
        	}
        }],
        buttonAlign:'center'
	});
};
Ext.extend(Doc.Edit, Ext.Window, {
	initWin:function(){
		//alert(Ext.util.JSON.encode(data));
		this.form.getForm().reset();
		this.userSelector==null;			
	},
	saveDoc:function(){
		if(!this.form.getForm().isValid()){
			Utils.info2("有必填项没有填写，请检查！",180);
			return;
		}
		var conn = new Ext.data.Connection({
  			url:'../superviseDoc/addDoc.form?timesnap=' + Date.parse(new Date()),
  			method:'post'
  		})
  		var param=this.form.getForm().getFieldValues();
  		//alert(Ext.util.JSON.encode(param));		
		Utils.request(conn,{
  			params:param,
  			scope:this,
  			success:function(data){
  				Utils.info2();
				this.scope.hide();
				this.scope.parentWin.queryDoc();
  			}
  		});
	},
	onSave: function() {
		this.saveDoc();
		/*scope=this;
		if(this.winMsg==null)
			this.winMsg=new Message.Msg(function(){
				scope.txtRemind.setValue(scope.winMsg.txtRemind);
				scope.saveDoc();
			},'你确实要保存吗？');
		this.winMsg.show();*/
	},
	showUser:function(){
		var scope=this;
		if(this.userSelector==null){
			var value=this.txtManagerId.getValue();
			this.userSelector=new Tree.User(value,function(o){
				scope.setUsers(o);
			},null,'110');
		}
		this.userSelector.show();
	},
	setUsers:function(o){
		var names = [];
		var ids =[];
		for(var i=0;i<o.length;i++) {
			names.push(o[i].text);
			ids.push(o[i].id);
		}
		this.txtManagerId.setValue(ids.join(','));
		this.txtManagerName.setValue(names.join(','));		
	}
});
